package com.example.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import com.example.demo.model.ResponseModal;
import com.example.demo.utils.ErrorLogsUtil;

@ControllerAdvice
public class ExceptionHandlers {
    Logger logger = LoggerFactory.getLogger(ExceptionHandlers.class);
    
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseModal handle(Exception e) {
        ErrorLogsUtil.error(e); //将异常写入数据库
        if (e instanceof Exception) {
//            Exception pillarException = (LpayException) e;
            return new ResponseModal(400,true,"scuessful",e.getCause());
        }else if (e instanceof MethodArgumentTypeMismatchException){
            return new ResponseModal(400,true,"scuessful",e.getCause());
        }else {
            logger.info(e.toString());
            e.printStackTrace();
            return new ResponseModal(400,true,"secussful");
        }
    }
}